package feeinterface.process;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.ulwx.tool.ArrayUtils;
import com.ulwx.tool.StringUtils;
import com.ulwx.type.TObject;

import feeinterface.protocol.utils.Constants;
import feeinterface.utils.PhoneUtils;


public class GateServerProcessor extends Processor {
	private static Logger log = Logger.getLogger(GateServerProcessor.class);
	@Override
	public void process(HttpServletRequest request,
			Map<String, String[]> parmMap,TObject bodyBytes) {
		// TODO Auto-generated method stub
		String gateKey ="";
		String gateIP = "";
		String phoneFromGateWay="";
		
	    String from=StringUtils.trim(ArrayUtils.getFirst(parmMap.get("RequestFrom")));
	    
	    if(from.equals(Constants.RequestFrom.SERVER+"")){//如果请求来自服务器
	    	gateKey = StringUtils.trim(ArrayUtils.getFirst(parmMap.get("GatewayKW")));
	    	gateIP = StringUtils.trim(ArrayUtils.getFirst(parmMap.get("GatewayIP")));
	    	phoneFromGateWay="";
	    }else{
	    	gateKey = StringUtils.trim(request.getHeader("Via"));
//	    	log.info("=========================gateKey:"+gateKey+";==================");
			gateIP = getRemoteAddr(request);
			phoneFromGateWay=getPhoneFromGateWay(request); 	
	    }

		
		if(StringUtils.containsIgnoreCase(gateKey, "WAP")){
			parmMap.put(Constants.PROTOCOL_REQ_GATEWAY_IS_CMWAP, new String[] { true+"" });
		}else{
			parmMap.put(Constants.PROTOCOL_REQ_GATEWAY_IS_CMWAP, new String[] { false+"" });
		}

		

		phoneFromGateWay =PhoneUtils.getAdjustedPhone(phoneFromGateWay);

		parmMap.put(Constants.PROTOCOL_REQ_GATEWAY_PHONE, new String[] { phoneFromGateWay });

		// 网关ip
		parmMap.put(Constants.PROTOCOL_REQ_GATEWAY_IP, new String[] { gateIP });

		// 网关关键字
		parmMap.put(Constants.PROTOCOL_REQ_GATEWAY_KEY, new String[] { gateKey });

		//log.debug("---"+CollectionUtils.toString(parmMap));

	}

	public static String getPhoneFromGateWay(HttpServletRequest request){
		String phoneFromGateWay=StringUtils.trim(request.getHeader("x-up-calling-line-id"));
		if(StringUtils.isEmpty(phoneFromGateWay)){
			phoneFromGateWay=StringUtils.trim(request.getHeader("X-Up-Calling-Line-ID"));
		}
		return phoneFromGateWay;
	}
	public static String getRemoteAddr(HttpServletRequest request) {
		String ip = "";
		try {
			ip = StringUtils.trim(request.getHeader("X-Real-IP"));
//
//			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
//				ip = StringUtils.trim(request.getHeader("x-forwarded-for"));
//			}
//			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
//				ip = StringUtils.trim(request.getHeader("Proxy-Client-IP"));
//			}
//			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
//				ip = StringUtils.trim(request.getHeader("WL-Proxy-Client-IP"));
//			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = StringUtils.trim(request.getRemoteAddr());
			}

			// 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串Ｉｐ值
			// 是取X-Forwarded-For中第一个非unknown的有效IP字符串
			String[] str = ip.split(",");
			if (str != null && str.length > 1) {
				ip = str[0];
			}
		} catch (Exception e) {
			log.error("",e);
		}

		return ip;

	}

}
